#!/usr/bin/env python3
# ui/info_table.py
# 机器人信息表格
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem, QHeaderView
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor, QFont

import logging

logger = logging.getLogger(__name__)


class RobotInfoTable(QTableWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setColumnCount(6)
        self.setHorizontalHeaderLabels(
            ["Robot", "当前位置", "初始位置", "目标位置", "状态", "更新"]
        )
        self.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive)
        self.verticalHeader().setVisible(False)
        self.setSelectionBehavior(QTableWidget.SelectRows)
        self.setEditTriggers(QTableWidget.NoEditTriggers)
        self.setColumnWidth(0, 80)
        self.setColumnWidth(1, 120)
        self.setColumnWidth(2, 120)
        self.setColumnWidth(3, 120)
        self.setColumnWidth(4, 80)
        self.setColumnWidth(5, 80)
        self.setStyleSheet(
            """
            QTableWidget {
                font-size: 10px;
                gridline-color: #e0e0e0;
            }
            QHeaderView::section {
                background-color: #f0f0f0;
                padding: 4px;
                border: 1px solid #d0d0d0;
                font-weight: bold;
            }
        """
        )

    def update_robots(self, robots):
        """更新表格"""
        self.setRowCount(len(robots))
        for row, robot in enumerate(robots):
            robot_str = f"{robot['id']} ({robot['role']})"
            name_item = QTableWidgetItem(robot_str)
            name_item.setTextAlignment(Qt.AlignLeft | Qt.AlignVCenter)
            self.setItem(row, 0, name_item)

            pos_item = QTableWidgetItem(robot.get("position", "未知"))
            self.setItem(row, 1, pos_item)

            initial_item = QTableWidgetItem(robot.get("initial_position", "未记录"))
            self.setItem(row, 2, initial_item)

            target_item = QTableWidgetItem(robot.get("target", "未设置"))
            self.setItem(row, 3, target_item)

            status = robot.get("status", "未知")
            status_item = QTableWidgetItem(status)
            status_item.setTextAlignment(Qt.AlignCenter)
            status_item.setForeground(
                QColor(0, 128, 0) if status == "active" else QColor(200, 0, 0)
            )
            self.setItem(row, 4, status_item)

            last_update_secs = robot.get("last_update", 0)
            update_item = QTableWidgetItem(f"{last_update_secs:.1f}s")
            update_item.setTextAlignment(Qt.AlignCenter)
            if last_update_secs > 10:
                update_item.setForeground(QColor(200, 0, 0))
            elif last_update_secs > 5:
                update_item.setForeground(QColor(200, 100, 0))
            self.setItem(row, 5, update_item)

            if status == "disconnected":
                for col in range(6):
                    item = self.item(row, col)
                    if item:
                        item.setBackground(QColor(245, 245, 245))
